<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册"></cp-nav-bar>
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- form 表单 -->
    <van-form autocomplete="off" @submit="onLogin" ref="form">
      <van-field
        placeholder="请输入手机号"
        type="tel"
        v-model="mobile"
        name="mobile"
        :rules="mobileRules"
      ></van-field>
      <van-field
        v-if="isPass"
        placeholder="请输入密码"
        v-model="password"
        :rules="passwordRules"
        :type="isShow ? 'text' : 'password'"
      >
        <template #button>
          <cp-icon
            :name="`login-eye-${isShow ? 'on' : 'off'}`"
            @click="isShow = !isShow"
            style="margin-right: 10px"
          ></cp-icon>
        </template>
      </van-field>
      <van-field v-else placeholder="短信验证码" :rules="codeRules" v-model="code">
        <template #button>
          <span v-if="time">{{ time }}s后再次发送</span>
          <span v-else class="btn-send" @click="send">发送验证码</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>

    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onUnmounted } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
// 表单提交
import { showToast, showSuccessToast, type FormInstance } from 'vant'
import { loginAPI, sendMobileCode } from '@/services/user'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores'

const store = useUserStore()
const router = useRouter()
const route = useRoute()

const mobile = ref<string>('13230000088')
const password = ref<string>('abc12345')
const code = ref<string>('')

const agree = ref<boolean>(false)
// 控制密码是否显示
const isShow = ref(false)
//控制是否短信登录
const isPass = ref(true)

const onLogin = async () => {
  if (!agree.value) return showToast('请勾选我已同意')
  const res = await loginAPI(mobile.value, password.value)
  //保存到用户store
  store.saveUser(res.data)
  // 验证完毕，进行登录
  // 跳转路由
  router.replace((route.query.returnUrl as string) || '/')
  // 提示用户
  showSuccessToast('登陆成功')
}
//获取vant里的form实例
const form = ref<FormInstance>()
//验证码倒计时
const time = ref(0)
let timeId: number
const send = async () => {
  // 已经倒计时time的值大于0，此时不能发送验证码
  if (time.value > 0) return
  await form.value?.validate('mobile')
  const { data } = await sendMobileCode(mobile.value, 'login')
  showSuccessToast('发送成功')
  code.value = data.code
  time.value = 5
  if (timeId) clearInterval(timeId)
  timeId = setInterval(() => {
    time.value--
    if (time.value === 0) clearInterval(timeId)
  }, 1000)
}
//页面卸载定时器需要清除
onUnmounted(() => {
  clearInterval(timeId)
})
</script>

<style lang="scss" scoped>
// van-form 下添加
.btn-send {
  color: var(--cp-primary);
}
.login {
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
}
</style>
